var loginApp = angular.module("loginApp", ["scService","angular-popups"]);
loginApp.controller("loginController", function($scope, $rootScope, pubFun,$http, $q, Popup,scCodeService, $timeout, $location, $stateParams, commonService, jsonToStr, $httpParamSerializerJQLike) {
		//alert("loginAPP")
        $rootScope.showLogin = false;
        $scope.phoneNumber;
        $scope.send = "验证";
        $scope.code;
        $scope.codeClick = true;
        $scope.couldClick = true;
        $scope.loginIng = false;
        $scope.isTimer = false; //判断定时器是否触发，是：解除验证手机 ng-keyup=checkCodeValid()
        var gotCode=false;
        //重发验证码计时器
        $scope.timer = function(wait) {
            var wait = wait;
            if (wait == 0) {
                $scope.send = "重新发送";
                wait = wait;
                $scope.isTimer = false;
                $scope.codeClick = false;
            } else {
                wait--;
                $scope.isTimer = true;
                $scope.codeClick = true;
                $scope.send = wait + "(s)"
                $timeout(function() {
                    $scope.timer(wait)
                }, 1000);
            }
        };
        //点击发送验证码按钮
        $scope.sendCode = function() {
            if (!(/^1[34578]\d{9}$/.test($scope.phoneNumber))) {

                $scope.toastSet({
                    content: "请输入正确手机号"
                })
            } else {
                var postData = {
                    "phone": $scope.phoneNumber,
                    "merchantCode": mainConfig.merchantCode
                }
                scCodeService.sendVerifyCode(postData).then(function(data) {
                    var datastr = JSON.stringify(data);
                    // alert(datastr);
                    if (data.code == "200") {
                        $scope.timer(60);
                        
                        Popup.notice('已发送', 1000);
                        gotCode = true;
                    } else if (data.code == "0106004") {
                        
                        Popup.notice('1分钟之内只能发送一次', 2000);
                    } else {
                        Popup.notice(data.msg, 2000);
                    }
                });
            }
        };

        $scope.login = function() {
            var deferred = $q.defer();
            $scope.couldClick = true; //禁点登录   
            $scope.loginIng = true; //loding弹出 
            var openId = localStorage.getItem("openId"); //|| parseInt(Math.random() * 1000000);
            localStorage.setItem("openId", openId)
            var postData = {
                "phone": $scope.phoneNumber,
                "verifyCode": $scope.code,
                "source": mainConfig.source,
                "merchantCode": mainConfig.merchantCode,
                "account": openId,
                "bindingType": "1"
            };
            scCodeService.login(postData).then(function(data) {
                $scope.couldClick = false; //取消禁点登录
                var datastr = JSON.stringify(data);
                //alert(datastr);
                if (data.code == "200") {
                      //alert("绑定成功")
                    deferred.resolve();
                } else if (data.code == "0101011") {
                    $scope.loginIng = false; //关闭loading   
                    Popup.notice("验证码错误", 2000);
                } else if (data.code == "0101302") {
                    $scope.loginIng = false; //关闭loading   
                    
                    Popup.notice("帐号已绑定", 2000);
                } else if(data.code == "0101015"){
                    $scope.loginIng = false; //关闭loading   
                    Popup.notice("验证码已过期", 2000);
                } else {
                    //alert(datastr)
                    $scope.loginIng = false; //关闭loading                       
                    Popup.notice("登录失败,请稍后重试", 2000);
                }
            });
            return deferred.promise;
        };
        //验证合性改变按钮颜色
        $scope.checkCodeValid = function() {
            $scope.codeClick = (/^\d{11}$/.test($scope.phoneNumber)) ? false : true;
        };
        $scope.checkValid = function() {
            $scope.couldClick = ((/^1[34578]\d{9}$/.test($scope.phoneNumber)) &&
                (/^(\d)+$/.test($scope.code))) ? false : true;
        };
        // 点击登录按钮
        $scope.loginFun = function() {
            //alert("点击"+$scope.gotCode)
            if (gotCode == true) {
                $scope.login().then(function() {
                    return pubFun.autoLogin();
                }).then(function() {
                    return pubFun.getUserCard();
                }).then(function() {
                    var state = localStorage.getItem("state")
                    if (state == '1') {
                        $location.path('userInfo')
                    } else if (state == '2') {
                        window.location.href = mainConfig.hostUrl + "scanCode.html";
                    }else{
                        window.location.reload();
                    }
                })
            } else {
                $scope.toastSet({
                    content: '请先获取验证码'
                })
            }
        };
        //获取openId
        $rootScope.getOpenId = function() {
            
            var deferred = $q.defer();
            var code = $stateParams.code;
            var postData = {
                "source": mainConfig.source,
                "merchantCode": mainConfig.merchantCode,
                "code": code
            };
            deferred.resolve()
            return deferred.promise;
        };

    })
    //有未支付订单页面的控制器
loginApp.controller('notPayController', function($scope, $location, $stateParams) {
    //点击未完成的订单
    var notPayCardNo = $stateParams.cardNo;
    $scope.goPay = function() {
        // alert("跳转支付页面"+notPayCardNo)
        // $location.path("orderPay/"+notPayCardNo);
        window.location.href = mainConfig.hostUrl + "index.html?#/orderPay/" + notPayCardNo;
    };
    
});
// 登录指令
loginApp.directive('login', function($rootScope) {
    return {
        restrict: 'EA',
        templateUrl: 'assets/scripts/plugins/weui-angular/login.html',
        replace: true,
        controller: 'loginController',
        link: function(scope) {
            scope.loginHide = function() {
                localStorage.removeItem("state")
                $rootScope.showLogin = false;
            };
        }

    }
});